//
//  CantripWorldSettingsSelector.m
//  Cantrip
//
//  Created by James Callender on Sun May 05 2002.
//  Copyright (c) 2001 James Callender. All rights reserved.
//

#import "CantripWorldSettingsSelector.h"

@implementation CantripWorldSettingsSelector

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
        settingsModules = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [settingsModules release];
    [super dealloc];
}

- (void)drawRect:(NSRect)rect 
{
    id <CantripSettingsModuleProtocol> module;
    unsigned int index;
    NSRect drawRect = NSMakeRect(0,0,0,0);

    drawRect.size.height = [self visibleRect].size.height;
    drawRect.size.width = Selector_IconWidth;

    for (index = 0; index < [settingsModules count]; index++) {
	NSSize imgSize;
	NSRect imgRect;
	module = [settingsModules objectAtIndex:index];
	
	imgSize = [[module settingsDisplayIcon] size];
	imgRect = NSMakeRect(NSMidX(drawRect) - imgSize.width/2, 20, imgSize.width, imgSize.height);
	
	if (glowingModule == module) {
	    [[NSColor colorWithCalibratedWhite:0.8 alpha:0.75] set];
	    NSRectFillUsingOperation(drawRect, NSCompositeSourceOver);
	}
	[[module settingsDisplayIcon] drawInRect:imgRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
	[[module settingsDisplayName] drawAtPoint:NSMakePoint(NSMidX(drawRect) - [[module settingsDisplayName] sizeWithAttributes:nil].width/2, 5) 
		withAttributes:nil];
	
	drawRect.origin.x += drawRect.size.width;
    }
}

- (void)mouseDown:(NSEvent *) event
{
    NSPoint eventLocation;
    int index;

    eventLocation = [self convertPoint:[event locationInWindow] fromView:nil];

    index = eventLocation.x / Selector_IconWidth;

    if (index < [settingsModules count]) {
	glowingModule = [settingsModules objectAtIndex:index];
    }

    [self autoscroll:event];
    [self setNeedsDisplay:YES];
}

- (void)mouseDragged:(NSEvent *)theEvent
{
    [self mouseDown:theEvent];
}

- (void)mouseUp:(NSEvent *)theEvent
{
    if (glowingModule != selectedModule) {
	selectedModule = glowingModule;
	if ([delegate respondsToSelector:@selector(changeModuleView:)])
	    [delegate changeModuleView:selectedModule];
    }
}

- (void)selectModuleAtIndex:(int)index
{
    if ([settingsModules count]) 
	glowingModule = selectedModule = (id <CantripSettingsModuleProtocol>)[settingsModules objectAtIndex:index];
}

- (void)setDelegate:(id)d
{
    delegate = d;
}

- (NSMutableArray *)modules
{
    return settingsModules;
}

- (id <CantripSettingsModuleProtocol>)selectedModule
{
    return selectedModule;
}

@end
